package com.scada.controller;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.scada.hessian.client.impl.MemberPlanBuyVisitorClient;
import com.scada.model.bean.MemberPlanBuy;
import com.scada.model.bean.ServiceInfo;
import com.scada.model.bean.User;
import com.scada.model.bean.vo.MemberPlanBuyVO;
import com.scada.model.dataTable.MemberPlanBuyList;
import com.scada.model.result.PageInfo;
import com.scada.model.result.ResponseBean;
import com.scada.model.search.MemberPlanBuySearch;

@Controller
@RequestMapping("planBuy")
public class MemberPlanBuyController extends BaseController{

	@Autowired
	private MemberPlanBuyVisitorClient memberPlanBuyVisitorClient;
	
	@RequestMapping(value = "toMemberMain")
	public String toMemberMain(){
		return "planBuy/member/list";
	}
	
	@RequestMapping(value = "getPlanBuyMemberPageData")
	@ResponseBody
	public String getPlanBuyMemberPageData(@ModelAttribute MemberPlanBuySearch memberPlanBuySearch) {
		
		PageInfo<MemberPlanBuyList> pageInfo = new PageInfo<MemberPlanBuyList>();
        pageInfo.setStart(memberPlanBuySearch.getStart());
        pageInfo.setPageSize(memberPlanBuySearch.getLength());
        pageInfo.setDraw(memberPlanBuySearch.getDraw());
        memberPlanBuySearch.setServiceType(ServiceInfo.ServiceTypeCode.MEMBER.getKey());
        ResponseBean rb = memberPlanBuyVisitorClient.getMemberPlanBuyPageData(memberPlanBuySearch);
        if(StringUtils.equals("2", rb.getType())){
        	pageInfo.setRecordsTotal((Integer)rb.getObject("resultCount"));
        	pageInfo.setData((List<MemberPlanBuyList>)rb.getObject("resultList"));
        }else{
        	pageInfo.setRecordsTotal(0);
        	pageInfo.setData(new ArrayList<MemberPlanBuyList>());
        }
        
		return toJson(pageInfo);
	}
	
	@RequestMapping(value = "toAddPlanBuyMember")
	public ModelAndView toAddPlanBuyMember(ModelMap model){
		
		return new ModelAndView("planBuy/member/add");
	}
	
	@RequestMapping(value = "insertMemberPlanBuy")
	@ResponseBody
	public String insertMemberPlanBuy(@ModelAttribute MemberPlanBuy memberPlanBuy){
		
		User user = getCurrentUser();
		memberPlanBuy.setCreateId(user.getId());
		memberPlanBuy.setLastUpdateId(user.getId());
		ResponseBean rb = memberPlanBuyVisitorClient.insertMemberPlanBuy(memberPlanBuy);
		return toJson(rb);
	}
	
	@RequestMapping(value = "toEditPlanBuy")
	public ModelAndView toEditPlanBuy(Long id, ModelMap model){
		
		MemberPlanBuyVO mpb = null;
		try {
			ResponseBean rb = memberPlanBuyVisitorClient.getMemberPlanBuyById(id);
			if(StringUtils.equals("2", rb.getType())){
				mpb = (MemberPlanBuyVO)rb.getObject("result");
			}
		}catch (Exception e) {
			
		}
		
		model.put("mpb", mpb);
		if(mpb != null) {
			if(StringUtils.equals(ServiceInfo.ServiceTypeCode.MEMBER.getKey(), mpb.getServiceType())) {
				return new ModelAndView("planBuy/member/edit");
			}else if(StringUtils.equals(ServiceInfo.ServiceTypeCode.PROJECT.getKey(), mpb.getServiceType())) {
				return new ModelAndView("planBuy/project/edit");
			}
		}
		
		return null;
		
	}
	
	
	@RequestMapping(value = "updateMemberPlanBuy")
	@ResponseBody
	public String updateMemberPlanBuy(@ModelAttribute MemberPlanBuy memberPlanBuy){
		
		User user = getCurrentUser();
		memberPlanBuy.setLastUpdateId(user.getId());
		ResponseBean rb = memberPlanBuyVisitorClient.updateMemberPlanBuy(memberPlanBuy);
		
		return toJson(rb);
	}
	
	@RequestMapping(value = "toProjectMain")
	public String toProjectMain(){
		return "planBuy/project/list";
	}
	
	@RequestMapping(value = "getPlanBuyProjectPageData")
	@ResponseBody
	public String getPlanBuyProjectPageData(@ModelAttribute MemberPlanBuySearch memberPlanBuySearch) {
		
		PageInfo<MemberPlanBuyList> pageInfo = new PageInfo<MemberPlanBuyList>();
        pageInfo.setStart(memberPlanBuySearch.getStart());
        pageInfo.setPageSize(memberPlanBuySearch.getLength());
        pageInfo.setDraw(memberPlanBuySearch.getDraw());
        memberPlanBuySearch.setServiceType(ServiceInfo.ServiceTypeCode.PROJECT.getKey());
        ResponseBean rb = memberPlanBuyVisitorClient.getMemberPlanBuyPageData(memberPlanBuySearch);
        if(StringUtils.equals("2", rb.getType())){
        	pageInfo.setRecordsTotal((Integer)rb.getObject("resultCount"));
        	pageInfo.setData((List<MemberPlanBuyList>)rb.getObject("resultList"));
        }else{
        	pageInfo.setRecordsTotal(0);
        	pageInfo.setData(new ArrayList<MemberPlanBuyList>());
        }
        
		return toJson(pageInfo);
	}
	
	@RequestMapping(value = "toAddPlanBuyProject")
	public ModelAndView toAddPlanBuyProject(ModelMap model){
		
		return new ModelAndView("planBuy/project/add");
	}
	
	@RequestMapping(value = "deleteMemberPlanBuyById")
	@ResponseBody
	public String deleteMemberPlanBuyById(String ids){
		
		ResponseBean rb = new ResponseBean();
		try{
			memberPlanBuyVisitorClient.deleteMemberPlanBuyById(MemberPlanBuy.IsShowCode.NO.getKey(), ids);
			rb.success();
		}catch(Exception e){
			rb.failed();
		}
		return toJson(rb);
	}
}
